package com.zlm.spider.service.impl;

import com.zlm.spider.dao.JobDao;
import com.zlm.spider.entity.Job;
import com.zlm.spider.service.JobService;
import com.zlm.spider.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import javax.transaction.Transactional;

/**
 * @author Harbour
 * @date 2021-04-13 9:05
 */
@Service
public class JobServiceImpl implements JobService {

    @Autowired
    private JobDao jobDao;

    @Override
    @Transactional
    public void save(Job job) {
        if (!isJobExist(job)) {
            jobDao.saveAndFlush(job);
        }
    }

    @Override
    public boolean isJobExist(Job job) {
        Jedis jedisClient = RedisUtils.getJedisClient();
        Long exist = jedisClient.sadd("job-set", job.getUrl() + job.getTime());
        jedisClient.close();
        return exist == 0L;
    }

}
